//
//  CollectViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/18.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectView.h"
#import <AVOSCloud.h>
#import <MediaPlayer/MediaPlayer.h>
#import "SkillInfoViewController.h"
@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    BOOL _isEditing;
}
@property (nonatomic,strong)CollectView *collectView;
@property (nonatomic,strong)NSMutableArray *collectBasketBallArray;
@property (nonatomic,strong)NSString *webViewUrl;
@end

@implementation CollectViewController
- (void)loadView
{
    self.collectView = [[CollectView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.collectView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectView.backgroundColor = ColorHex(0x0d1c38);
    //返回按钮
    [self.collectView.cancleButton addTarget:self action:@selector(returnBeforeAction) forControlEvents:UIControlEventTouchUpInside];
    //注册重用
    [self.collectView.collectTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //遵守协议
    self.collectView.collectTableView.delegate = self;
    self.collectView.collectTableView.dataSource = self;
    //编辑按钮
    [self.collectView.editButton addTarget:self action:@selector(writenItem:) forControlEvents:UIControlEventTouchUpInside];
    //添加手势
    UIScreenEdgePanGestureRecognizer *edgeGR = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgeGR)];
    [self.collectView addGestureRecognizer:edgeGR];
    self.collectView.userInteractionEnabled = YES;
    edgeGR.edges = UIRectEdgeLeft;
    
}
#pragma mark - 取消按钮方法
- (void)returnBeforeAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    AVUser *current = [AVUser currentUser];
    self.collectBasketBallArray = [current objectForKey:@"BasketBall"];
    if (self.collectBasketBallArray.count == 0) {
        self.collectView.collectTableView.hidden = YES;
        self.collectView.noneLabel.hidden = NO;
    }
    
}
#pragma mark - tableView dataSource 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.collectBasketBallArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = self.collectBasketBallArray[indexPath.row];
    
    cell.textLabel.text = [dic valueForKey:@"title"];
    return cell;
}


#pragma mark - 编辑1.2
- (void)writenItem:(UIButton *)sender
{
    //第一步:让tableView进入编辑状态
    [self.collectView.collectTableView setEditing:_isEditing animated:YES];
    //第二步:改变item的title
    if (_isEditing == YES) {
        [sender setTitle:@"完成" forState:UIControlStateNormal];
        _isEditing = NO;
    }else{
        [sender setTitle:@"编辑" forState:UIControlStateNormal];
        _isEditing = YES;
    }
    
}

#pragma mark - 编辑2询问datasource是否可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#pragma mark - 编辑3 样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 1;
}
#pragma mark - 编辑 添加方法(删除 添加)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除数据源
    //1.找到要删除的数据

    AVUser *current = [AVUser currentUser];
    if (indexPath.section == 0) {
        if (UITableViewCellEditingStyleDelete == editingStyle) {                
                NSArray *array = [current objectForKey:@"BasketBall"];
                NSDictionary *dic = array[indexPath.row];
                [self.collectBasketBallArray removeObjectAtIndex:indexPath.row];
                [current removeObject:dic forKey:@"BasketBall"];
                [current saveInBackground];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:4];
                [tableView reloadData];

        }
    }
}

#pragma mark - 点击cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.collectBasketBallArray[indexPath.row];
    NSString *urlStr = dic[@"url"];
    NSString *str = [urlStr substringToIndex:10];
    if ([str isEqualToString:@"http://api"]) {
        SkillInfoViewController *skillInfoVC = [[SkillInfoViewController alloc]init];
        NSString *strID = [urlStr substringFromIndex:urlStr.length - 9];
        skillInfoVC.modalTransitionStyle = 0;
        skillInfoVC.webViewUrl = urlStr;
        skillInfoVC.passValueId = [strID integerValue];
        [self presentViewController:skillInfoVC animated:YES completion:nil];
    }else{
        MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
        [playerVC.moviePlayer prepareToPlay];
        playerVC.moviePlayer.shouldAutoplay = YES;
        [self presentViewController:playerVC animated:YES completion:nil];
    }
    
}
#pragma mark - 手势方法实现
- (void)edgeGR
{
    [self dismissViewControllerAnimated:YES completion:nil];
}















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
